﻿using DevExpress.XtraEditors;
using ImageDefine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TableModule;

namespace CustomModule
{
    public partial class FormStart : DevExpress.XtraEditors.XtraForm
    {
        public FormStart()
        {
            InitializeComponent();
        }

        private void FormStart_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void FormStart_Load(object sender, EventArgs e)
        {
            CustomManager.SmartDisplayControl = smartDisplayControl1;
            
            ConveyerManager.Init();
            timer1.Enabled = true;
            if (FrameManager.MachineStatus.Is4Axis)
            {
                simpleButton计算针头和相机距离关系.Visible = false;
                checkEditPositionMatch.Visible = false;
            }

            textEditOffSetX.Text = TableManager.tableRunner.GetPosition("偏移设定").dXPos.ToString();
            textEditOffSetY.Text = TableManager.tableRunner.GetPosition("偏移设定").dYPos.ToString();
            textEditOffSetZ.Text = TableManager.tableRunner.GetPosition("偏移设定").dZPos.ToString();


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textEditBeltCurrentPos.Text = ConveyerManager.CurrentPos.ToString("0.000")+"__"+ConveyerManager.productItems.Count.ToString();
            ConveyerManager.ShowProduct(smartDisplayControl2);
            checkEdit称重请求.Checked = CustomManager.bRequestGlueWeight;
            if (UserModule.UserManager.iUserLevel > 0)
            {
                if (FrameManager.MachineStatus.Is4Axis)
                {
                    simpleButton计算针头和相机距离关系.Visible = false;
                    checkEditPositionMatch.Visible = false;
                    groupControlNozzleCalib.Visible = false;
                }
                else
                {
                    simpleButton计算针头和相机距离关系.Visible = true;
                    checkEditPositionMatch.Visible = true;
                    groupControlNozzleCalib.Visible = true;
                }
                    
            }
            else
            {
                 simpleButton计算针头和相机距离关系.Visible = false;
                checkEditPositionMatch.Visible = false;
                groupControlNozzleCalib.Visible = false;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            CustomManager.zMotionCard.StartAdd();
        }

        private void simpleButton称重请求_Click(object sender, EventArgs e)
        {
            if(CustomManager.bRequestGlueWeight)
                CustomManager.bRequestGlueWeight = false;
            else
                CustomManager.bRequestGlueWeight = true;
        }

        private void checkEdit称重请求_Click(object sender, EventArgs e)
        {
            if (CustomManager.bRequestGlueWeight)
                CustomManager.bRequestGlueWeight = false;
            else
                CustomManager.bRequestGlueWeight = true;

            checkEdit称重请求.Checked = CustomManager.bRequestGlueWeight;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            //double dZoffSet = TableModule.TableManager.tableRunner.GetPosition("定位区域离皮带高度").dZPos;
            //ImageCalibModule.ImageCalibManager.ImageCalibHandeye.SetZOffSet(dZoffSet);
            ////dTragPos=ConveyerManager.CurrentPos
            //ImageTask imageTask = ImageTaskModule.ImageTaskManager.ImageTask("ProductDetect");
            //imageTask.Run(smartDisplayControl1);
            //if (imageTask.bResultOK)
            //{
            //    for (int i = 0; i < imageTask.ImageTaskResults.Count; i++)
            //    {
            //        ProductItem productItemNew = new ProductItem();
            //        productItemNew.TrigerBeltPosX = ConveyerManager.CurrentPos;
            //        if (FrameManager.MachineStatus.Is4Axis)
            //        {
            //            productItemNew.ImagePosX = imageTask.ImageTaskResults[i].ResultX;
            //            productItemNew.ImagePosY = imageTask.ImageTaskResults[i].ResultY;
            //            productItemNew.ImagePosU = imageTask.ImageTaskResults[i].ResultA;
            //        }
            //        else
            //        {

            //            productItemNew.ImagePosX = imageTask.ImageTaskResults[i].ResultX + TableManager.tableRunner.GetPosition("针头工具位置").dXPos;
            //            productItemNew.ImagePosY = imageTask.ImageTaskResults[i].ResultY + TableManager.tableRunner.GetPosition("针头工具位置").dYPos; ;
            //            productItemNew.ImagePosU = imageTask.ImageTaskResults[i].ResultA;
            //        }
            //        ConveyerManager.AddNewItem(productItemNew);
            //        FrameManager.FrameManager.ProductSetting.Input++;
            //    }
            //}
            if (ConveyerManager.productItems.Count > 0)
            {
                TableDataPosition pos = TableManager.tableRunner.WhereTable();
                ProductItem productItem = ConveyerManager.productItems[ConveyerManager.productItems.Count-1];
                double dActualyX = pos.dXPos - (ConveyerManager.CurrentPos - productItem.TrigerBeltPosX);
                double dActualyY = pos.dYPos;
                ImageTask imageTask = ImageTaskModule.ImageTaskManager.ImageTask("ProductDetect");
                double dMatchX = dActualyX- imageTask.dResultX;
                double dMatchY = dActualyY- imageTask.dResultY;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["三轴相机中心和针头偏移"].dXPos = dMatchX;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["三轴相机中心和针头偏移"].dYPos = dMatchY;

                TableManager.Save();

            }
        }

        private void checkEditPositionMatch_CheckedChanged(object sender, EventArgs e)
        {
            
                CustomManager.bRequestPositionMatch = checkEditPositionMatch.Checked;
           
        }

        private void checkEdit称重请求_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void textEditOffSetX_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                TableManager.tableActioner.tableDate.dicTableDataPos["偏移设定"].dXPos = double.Parse(textEditOffSetX.Text);
                TableManager.tableActioner.tableDate.dicTableDataPos["偏移设定"].dYPos = double.Parse(textEditOffSetY.Text);
                TableManager.tableActioner.tableDate.dicTableDataPos["偏移设定"].dZPos = double.Parse(textEditOffSetZ.Text);
                TableManager.Save() ;
            }
            catch 
            {
                textEditOffSetX.Text = TableManager.tableRunner.GetPosition("偏移设定").dXPos.ToString();
                textEditOffSetY.Text = TableManager.tableRunner.GetPosition("偏移设定").dYPos.ToString();
                textEditOffSetZ.Text = TableManager.tableRunner.GetPosition("偏移设定").dZPos.ToString();
            }
            

        }
    }
}
